#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>

#include "Game.h"

void		Game::cam_move(int x, int y)
{
  long double	xt = x*(_xdim*1.0/this->_zoom)/3 + this->_cam.x;
  long double	yt = y*(_ydim*1.0/this->_zoom)/3 + this->_cam.y;

  this->_cam.x = xt;
  this->_cam.y = yt;
  std::cout << "Cam moving to " << this->_cam.x << " " << this->_cam.y << std::endl;
}

void		Game::applyCameraZoom(Coord& cursorPos, int newZoom)
{
  /*
  this->_cam.x = (this->_cam.x + cursorPos.x/this->_zoom) - (_xdim / newZoom) / 2;
  this->_cam.y = (this->_cam.y + cursorPos.y/this->_zoom) - (_ydim / newZoom) / 2;
  */
  if (newZoom == 0)
    return;
  this->_cam.x = (this->_cam.x + cursorPos.x/this->_zoom) - (_xdim / newZoom) / 2;
  this->_cam.y = (this->_cam.y + cursorPos.y/this->_zoom) - (_ydim / newZoom) / 2;
  this->_zoom = newZoom;
}

void		Game::updateMousePos()
{
  int		x;
  int		y;
  
  SDL_GetMouseState(&x, &y);
  this->_mouse.x = x;
  this->_mouse.y = y;
}

int		Game::manage_keyboard()
{
  Uint8*	keystate;
  static Coord* center = NULL;

  if (center == NULL)
    {
      center = new Coord();
      center->x = _xdim / 2;
      center->y = _ydim / 2;
    }
  keystate = SDL_GetKeyState(NULL);
  if (keystate[SDLK_UP])
    cam_move(0, -1);
  if (keystate[SDLK_DOWN])
    cam_move(0, 1);
  if (keystate[SDLK_LEFT])
    cam_move(-1, 0);
  if (keystate[SDLK_RIGHT])
    cam_move(1, 0);
  if (keystate[SDLK_ESCAPE])
    return (0);
  if (keystate[SDLK_PAGEUP])
    this->applyCameraZoom(*center, this->_zoom * 2);
  if (keystate[SDLK_PAGEDOWN])
    this->applyCameraZoom(*center, this->_zoom / 2);
  if (keystate[SDLK_s])
    sleep(1);
  if (keystate[SDLK_p])
    {
      if (this->_paused)
	_paused = false;
      else
	_paused = true;
    }
  if (keystate[SDLK_RETURN])
    {
      if (_edges)
	this->_edges = 0;
      else
	this->_edges = 1;
    }
  return (1);
}

void		Game::manage_mouse(SDL_Event *event)
{
  /* Deplacement mouse drag
  cam_move(-1 * (event->button.x - this->_mouse.x),
	   -1 * (event->button.y - this->_mouse.y));
  */
  this->_mouse.x = event->button.x;
  this->_mouse.y = event->button.y;
}

int		Game::manage_keys()
{
  SDL_Event		event;

  if (SDL_PollEvent(&event))
    {
      this->updateMousePos();
      if (event.type == SDL_QUIT)
	return (0);
      else if (event.type == SDL_KEYDOWN)
	{
	  if (!manage_keyboard())
	    return (0);
	}
      else if (event.type == SDL_MOUSEBUTTONDOWN)
	{
	  if (event.button.button == SDL_BUTTON_LEFT)
	    {
	      this->_clicked = 1;
	      std::cout << "xmouse=" << this->_cam.x + this->_mouse.x/this->_zoom
			<< " ymouse=" << this->_cam.y + this->_mouse.y/this->_zoom
			<< ", check this man, zooming to top left corner is quite OK !" << std::endl;
	    }
	  else if (event.button.button == SDL_BUTTON_WHEELUP)
	    this->applyCameraZoom(this->_mouse, this->_zoom * 2);
	  else if (event.button.button == SDL_BUTTON_WHEELDOWN)
	    this->applyCameraZoom(this->_mouse, this->_zoom / 2);
	}
      else if (event.type == SDL_MOUSEBUTTONUP)
	this->_clicked = 0;
      //      else if (event.type == SDL_MOUSEMOTION && this->_clicked)
      //	manage_mouse(&event);
    }
  return (1);
}

void		Game::ini_keys()
{
  SDL_EnableKeyRepeat(100, 20);
}
